// /**
//  * Created by python on 19-8-4.
//  *
//  */
// // var vm = new Vue({
// //     el: '#personalapps',
// //     data: {
// //
// //     }})
//
//


var vm = new Vue({
    el: '#apps',
    data: {
        HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        nick_name: '请添加',
        // mobile: '',
        address: '请添加',
        school: '请添加',
        company: '请添加',
        persurl: '请添加',
        persintro: '请添加',
        city: true,
        cityname: true,
        schoolname: true,
        companyname: true,
        persurlname: true,
        persintroname: true,
        cityname: true,

        change_city: false,
        change_school: false,
        change_company: false,
        change_persurl: false,
        change_persintro: false,
        newId: '',
        major:'',
        position:'',

        // email: '',
        // email_active: false,
        // set_email: false,
        // send_email_btn_disabled: false,
        // send_email_tip: '重新发送验证邮件',
        // email_error: false,
        // histories: []
    },
    mounted: function () {


        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios.get(this.HOST + 'user/', {
                // 向后端传递JWT token的方法

                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json',
            })
                .then(response => {

                    // 加载用户数据
                    // console.log(response);
                    // this.user_id = response.data.id;
                    this.nick_name = response.data.nick_name;
                    this.address = response.data.address;
                    this.company = response.data.company;
                    this.persurl = response.data.persurl;
                    this.persintro = response.data.persintro;
                    this.city = response.data.city;
                    this.school = response.data.school;
                    // this.email = response.data.email;
                    // this.email_active = response.data.email_active;
                    // 补充请求浏览历史
                    // axios.get(this.HOST + '/browse_histories/', {
                    //         headers: {
                    //             'Authorization': 'JWT ' + this.token
                    //         },
                    //         responseType: 'json'
                    //     })
                    //     .then(response => {
                    //         this.histories = response.data;
                    //         for(var i=0; i<this.histories.length; i++){
                    //             this.histories[i].url = '/goods/' + this.histories[i].id + '.html';
                    //         }
                    //     })
                })
                .catch(error => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = '/login.html?next=/person-homepage.html';
                    }
                });
        } //else {
        //     location.href = '/login.html?next=/person-homepage.html';
        // }
    },
    methods: {


        city_set: function () {
            this.cityname = false;
            this.change_city = true
            this.$nextTick(() => {
                this.$refs.gain1.focus()
            })
            return false
        },
        show_city: function () {
            this.cityname = true;
            this.change_city = false
            return false

        },
        school_set: function () {
            this.schoolname = false;
            this.change_school = true
            this.$nextTick(() => {
                this.$refs.gain2.focus()
            })
        },
        show_school: function () {
            this.schoolname = true;
            this.change_school = false

        },
        company_set: function () {
            this.companyname = false;
            this.change_company = true
            this.$nextTick(() => {
                this.$refs.gain3.focus()
            })
        },
        show_company: function () {
            this.companyname = true;
            this.change_company = false

        },
        persurl_set: function () {
            this.persurlname = false;
            this.change_persurl = true
            this.$nextTick(() => {
                this.$refs.gain4.focus()
            })
        },
        show_persurl: function () {
            this.persurlname = true;
            this.change_persurl = false

        },
        persintro_set: function () {
            this.persintroname = false;
            this.change_persintro = true
            this.$nextTick(() => {
                this.$refs.gain5.focus()
            })

        },
        show_persintro: function () {
            this.persintroname = true;
            this.change_persintro = false

        },
        //隐藏所有表单

        hide_all_form:function () {
            this.change_city= false,
             this.change_school= false,
             this.change_company = false,
             this.change_persurl = false,
             this.change_persintro = false,
            this.cityname= true,
            this.schoolname= true,
            this.companyname= true,
            this.persurlname= true,
            this.persintroname= true
        },
        // 修改城市hi
        on_submit_city: function(){
            alert('heh')


            if (this.city) {
                axios.patch(this.HOST + 'city/', {
                        city: this.city
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-homepage.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }

            return false
        },
        // 修改学校
        on_submit_school: function() {
            alert('heh')


            if (this.city) {
                axios.patch(this.HOST + 'school/', {
                        school: this.school + this.major
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-homepage.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
          // 修改公司
        on_submit_company: function() {
            alert('heh')


            if (this.city) {
                axios.patch(this.HOST + 'company/', {
                        company: this.company + this.position
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-homepage.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
          // 修改个人网址
        on_submit_persurl: function() {
            alert('heh')


            if (this.city) {
                axios.patch(this.HOST + 'persurl/', {
                        persurl: this.persurl
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-homepage.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
          // 修改个人简介
        on_submit_persintro: function() {
            alert('heh')


            if (this.city) {
                axios.patch(this.HOST + 'persintro/', {
                        persintro: this.persintro
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-homepage.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },


        //     // 退出
        //     logout: function(){
        //         sessionStorage.clear();
        //         localStorage.clear();
        //         location.href = '/login.html';
        //     },
        // 保存email
        //  save_email: function(){
        //      var re = /^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
        //      if(re.test(this.email)) {
        //          this.email_error = false;
        //      } else {
        //          this.email_error = true;
        //          return;
        //      }
        //      axios.put(this.HOST + '/email/',
        //          { email: this.email },
        //          {
        //              headers: {
        //                  'Authorization': 'JWT ' + this.token
        //              },
        //              responseType: 'json'
        //          })
        //          .then(response => {
        //              this.set_email = false;
        //              this.send_email_btn_disabled = true;
        //              this.send_email_tip = '已发送验证邮件'
        //          })
        //          .catch(error => {
        //              alert(error.data);
        //          });
        //  }
    }
});
